package com.ai.aicontact.common.tag;

import java.util.HashMap;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;

import com.ai.aicontact.common.context.ContextHolder;
import com.ai.aicontact.common.entity.SysStaticData;
import com.ai.aicontact.common.service.SysStaticDataService;

public class SelectStaticDataTag extends TagSupport{
	
	private static final long serialVersionUID = -3019482475793118426L;
 
	private String id;
	private String name;
	private String disabled; 
	private String selectClass;
	private String style;
	private String codetype;
	private String defaultValue;
	private String defaultText;
	private String selected; 
	
	private SysStaticDataService sysStaticDataService;

	
	public int doStartTag() throws JspException {
		StringBuilder sb = new StringBuilder();
		try{
			if (StringUtils.isEmpty(this.codetype)) {
				return -1;
			}
			if(sysStaticDataService == null){
				sysStaticDataService = ContextHolder.getBean("sysStaticDataServiceImpl");
			}
			List<SysStaticData> caches = sysStaticDataService.getStaticDataByCodyType(this.codetype);
			
			sb.append("<select ");
			if(StringUtils.isNotBlank(this.id)){
				sb.append("id=\"" + this.id +"\"");
			}
			if(StringUtils.isNotBlank(this.name)){
				sb.append(" name=\"" + this.name +"\"");
			}
			if(StringUtils.isNotBlank(this.disabled)){
				sb.append(" disabled=\"" + this.disabled +"\"");
			} 
			if(StringUtils.isNotBlank(this.selectClass)){
				sb.append(" class=\"" + this.selectClass +"\"");
			}
			if(StringUtils.isNotBlank(this.style)){
				sb.append(" style=\"" + this.style +"\"");
			}
			sb.append(">");
			
			if(StringUtils.isNotBlank(this.defaultValue)){
				 
				sb.append("<option value=\""+ this.defaultValue.trim() + "\" >");
				sb.append(this.defaultText);
				sb.append("</option>");
			}
			for (SysStaticData sysStaticData : caches) {
				sb.append("<option value=\""+ sysStaticData.codeValue+"\"");
				if((StringUtils.isNotBlank(this.selected)) && (this.selected.equals(sysStaticData.codeValue.toString()))){
					sb.append(" selected=\"selected\"");
				}
				sb.append(">");
				sb.append(sysStaticData.codeName);
				sb.append("</option>");
			}  
			sb.append("</select>");
			
			this.pageContext.getOut().write(sb.toString());
		}catch(Exception e){
			e.printStackTrace();
		}
		return 0;
	}
	public void release() {
		this.codetype = null;
		super.release();
	}
	
	public int doEndTag() throws JspException {
		return 6;
	}
	public String getStyle() {
		return style;
	}
	public void setStyle(String style) {
		this.style = style;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getcodetype() {
		return codetype;
	}
	public void setcodetype(String codetype) {
		this.codetype = codetype;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDisabled() {
		return disabled;
	}
	public void setDisabled(String disabled) {
		this.disabled = disabled;
	} 
	public String getSelectClass() {
		return selectClass;
	}
	public void setSelectClass(String selectClass) {
		this.selectClass = selectClass;
	}
	public String getDefaultValue() {
		return defaultValue;
	}
	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}
	public String getDefaultText() {
		return defaultText;
	}
	public void setDefaultText(String defaultText) {
		this.defaultText = defaultText;
	}
	public String getSelected() {
		return selected;
	}
	public void setSelected(String selected) {
		this.selected = selected;
	}
}
